package cn.kgc.ebook.controller;

import cn.kgc.ebook.pojo.Ebookentry;
import cn.kgc.ebook.pojo.ResultEntity;
import cn.kgc.ebook.service.EbookKentryService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class EntryController {

    @Resource
    EbookKentryService ebookKentryService;
    @RequestMapping("/selectEntry")
    public ResultEntity<PageInfo<Ebookentry>> seleEntry(@RequestParam(value = "categoryId",required = false,defaultValue = "0") Integer categoryId,
                                                    @RequestParam(value = "pageNum",required = false,defaultValue = "1") Integer pageNum,
                                                    @RequestParam(value = "pageSize",required = false,defaultValue = "1") Integer pageSize){
        PageInfo<Ebookentry> select = ebookKentryService.select(pageNum, pageSize, categoryId);
    return ResultEntity.successWithoutData(select);
    }

    //前段请求post,参数json必须添加@requestBody
    @RequestMapping("/saveEntry")
    public ResultEntity<Integer> saveEntry(@RequestBody Ebookentry ebookentry){

        Integer update;
        if(ebookentry.getId()==null){
     //添加
          update =  ebookKentryService.add(ebookentry);
     }else{
          update = ebookKentryService.update(ebookentry);
     }
//        Integer add = ebookKentryService.add(ebookentry);
    return ResultEntity.successWithoutData(update);
    }

    //根据id查图书
    @RequestMapping("/getEntryById")
    public ResultEntity<Ebookentry> getEntryId(Integer id){
        Ebookentry ebookentry = ebookKentryService.seleById(id);
    return ResultEntity.successWithoutData(ebookentry);
    }

    //删除图书
    @RequestMapping("/delEntryById")
    public ResultEntity<String> deleEntryId(Integer id){
        try {
            Integer delete = ebookKentryService.delete(id);
            return ResultEntity.successWithoutData(delete+"");
        }catch (Exception ex){
           return ResultEntity.failed(ex.getMessage());
        }

    }

}
